package com.taptech.doufu.ui.view.drawcircle;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.taptech.doufu.util.TTLog;

/**
 * Created by fishboneLsy on 2015/6/26.
 */
public class MyRecyclerView extends RecyclerView {

    private final int PULL_HEAD_VIEW_SPEED = 3;

    Context mContext;

    //当recyclerview距离底部toTopSpacing时，即判定recyclerview已到底部
    private int toBottomSpacing = 0;
    private boolean loadMoreEnable = false;
    //当recyclerview距离顶部toTopSpacing时，即判定recyclerview已到顶部
    private int toTopSpacing = 5;

    private RecyclerViewAdapterAsListView adapter;


    // 下拉刷新相关
    float moveStartY = 0;
    private boolean isRefreshEnable = false;
    View recyclerHeadView; // 头部view
    private int headViewHeight = 100;


    public MyRecyclerView(Context context) {
        super(context);
        initRecyclerView(context);
    }

    public MyRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRecyclerView(context);
    }

    public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initRecyclerView(context);
    }

    private void initRecyclerView(Context context) {
        mContext = context;
        //设置滚动监听事件
        this.addOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                TTLog.d("tag", "这个listview的newState=" + newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                boolean isBottom = false;
                TTLog.d("tag", "这个listview正在滑动" + computeVerticalScrollOffset());
                if (loadMoreEnable && onScrollToBottomListener != null) {
                    //如果已经滑到底部 scroll范围-(scroll高度+滑动的高度 ) <= toBottomSpacing
                    if (computeVerticalScrollRange() >= computeVerticalScrollExtent() && (computeVerticalScrollRange() - computeVerticalScrollExtent() - computeVerticalScrollOffset() <= toBottomSpacing)) {
                        onScrollToBottomListener.loadMore();
                        isBottom = true;
                    }
                }
                if (onScrollToTopListener != null){
                    if (computeVerticalScrollOffset() < 10){
                        onScrollToTopListener.refresh();
                    }else {
                        if (!isBottom){
                            onScrollToTopListener.notAtTopInScrollView(computeVerticalScrollOffset() , dy);
                        }

                    }
                }
            }

        });

    }

    public void setHeadViewHeight(int headViewHeight) {
        this.headViewHeight = headViewHeight;
    }



    public interface OnPullListener {
        void onPullRefresh();
    }

    OnPullListener pullListener;

    /**
     * 设置下拉刷新监听
     *
     * @param listener 下拉监听
     * @param layoutId 下拉顶部视图
     */
    public void setPullRefreshListener(int layoutId, OnPullListener listener) {
        if (listener != null) {
            pullListener = listener;
            isRefreshEnable = true;
            adapter = (RecyclerViewAdapterAsListView) MyRecyclerView.this.getAdapter();
            recyclerHeadView = LayoutInflater.from(mContext).inflate(layoutId, null);
            final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
            recyclerHeadView.setLayoutParams(params);
            adapter.addHeaderView(recyclerHeadView , 0);
            adapter.notifyDataSetChanged();

            this.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent ev) {
                    switch (ev.getAction()) {
                        case MotionEvent.ACTION_MOVE:

                            if (moveStartY == 0) {
                                moveStartY = ev.getRawY();
                            } else {

                                float tempHeight = ev.getRawY() - moveStartY;
                                if (tempHeight > 0) {
                                    adjustHeadViewHeight((int) (tempHeight/PULL_HEAD_VIEW_SPEED));
                                } else {
                                    params.height = 0;
                                    moveStartY = 0;
                                }
                            }
                            break;
                        case MotionEvent.ACTION_UP:
                            moveStartY = 0;
                            adjustHeadViewHeight(headViewHeight);
                            pullListener.onPullRefresh();
                            break;


                    }
                    return false;
                }
            });
        }
    }

    public void refreshComplete(){
        moveStartY = 0;
        adjustHeadViewHeight(0);
    }


    /**
     * 调整下拉刷新高度
     *
     * @param height
     */
    private void adjustHeadViewHeight(int height) {

        ViewGroup.LayoutParams params = recyclerHeadView.getLayoutParams();
        params.height = height;
        recyclerHeadView.setLayoutParams(params);
    }


    /**
     * RecyclerView滚到到顶部的监听
     */
    public interface OnScrollToTopListener {
        public void refresh();

        public void notAtTopInScrollView(int offsetY , int directionY);
    }

    public OnScrollToTopListener onScrollToTopListener = null;

    public void setOnScrollToTopListener(OnScrollToTopListener onScrollToTopListener) {
        this.onScrollToTopListener = onScrollToTopListener;
    }

    /**
     * RecyclerView滚动到底部的监听
     */
    public interface OnScrollToBottomListener {
        public void loadMore();
    }

    public OnScrollToBottomListener onScrollToBottomListener = null;

    public void setOnScrollToBottomListener(OnScrollToBottomListener onScrollToBottomListener) {
        this.loadMoreEnable = true;
        this.onScrollToBottomListener = onScrollToBottomListener;
    }


    public void setLoadMoreEnable(boolean loadMoreEnable) {
        this.loadMoreEnable = true;
    }

}
